﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using EasyLucky.Common;
using EasyLucky.Dal.Dal;
using EasyLucky.Dal.Entity;
using EasyLucky.Manager.Models;
using EasyLucky.Message.Messages;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace EasyLucky.Manager.ViewModels
{
    public class GiftManagerViewModel : ObservableObject
    {
        private GiftManagerDataM  dataM;

        public GiftManagerDataM DataM
        {
            get { return dataM; }
            set { SetProperty(ref dataM , value); }
        }

        private GiftDal giftDal;


        public GiftManagerViewModel()
        {
            this.DataM = new GiftManagerDataM()
            {
                Gifts = new System.Collections.ObjectModel.ObservableCollection<Common.Gift>(),
                NewGift = new Common.Gift() { },
                NewVisibility=System.Windows.Visibility.Collapsed,
            };
            
        }

        private void InitInfo()
        {
            this.DataM.Gifts.Clear();
            var entities = this.giftDal.QueryAll();
            foreach (var entity in entities)
            {
                var gift = new Gift()
                {
                    Id = entity.Id,
                    Name = entity.Name,
                    ImageUrl = entity.ImageUrl,
                };
                this.DataM.Gifts.Add(gift);
            }
        }

        #region Loaded

        private IRelayCommand loadedCommand;

        public IRelayCommand LoadedCommand
        {
            get { return loadedCommand ??= new RelayCommand(Loaded); }
        }

        private void Loaded()
        {
            if (giftDal == null)
            {
                this.giftDal = new GiftDal();
                this.InitInfo();
            }
        }

        #endregion

        #region 查询

        private IRelayCommand queryCommand;

        public IRelayCommand QueryCommand
        {
            get { return queryCommand ??= new RelayCommand(Query); }
        }

        private void Query()
        {
            this.InitInfo();
        }

        #endregion

        #region 新增

        private IRelayCommand newCommand;

        public IRelayCommand NewCommand
        {
            get { return newCommand ??= new RelayCommand(New); }
        }

        private void New()
        {
            this.DataM.NewVisibility = System.Windows.Visibility.Visible;
            this.DataM.NewGift=new Gift();
        }

        private IRelayCommand browserCommand;

        public IRelayCommand BrowserCommand
        {
            get { return browserCommand ??= new RelayCommand(Browser); }
        }

        private void Browser()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PNG(*.png)|*.png|JPG(*.jpg)|*.jpg|Gif(*.gif)|*.gif";
            if (openFileDialog.ShowDialog()==true)
            {
                var dest = Path.Combine(Environment.CurrentDirectory,"images", Path.GetFileName(openFileDialog.FileName));
                File.Copy(openFileDialog.FileName, dest,true);
                this.DataM.NewGift.ImageUrl = dest.Replace(Environment.CurrentDirectory, string.Empty);
            }
        }

        private IRelayCommand saveCommand;

        public IRelayCommand SaveCommand
        {
            get { return saveCommand ??= new RelayCommand(Save); }
        }

        private void Save()
        {
            if(string.IsNullOrEmpty( this.DataM.NewGift.Name)|| string.IsNullOrEmpty(this.DataM.NewGift.ImageUrl))
            {
                return;
            }
            var gift = new GiftEntity()
            {
                Id=this.DataM.NewGift.Id,
                Name = this.DataM.NewGift.Name,
                ImageUrl = this.DataM.NewGift.ImageUrl,
            };
            bool flag = false;
            if (gift.Id > 0)
            {
                int cnt = this.giftDal.UpdateGift(gift);
                if (cnt > 0)
                {
                    flag= true;
                }
            }
            else
            {
                int id = this.giftDal.AddGift(gift);
                if (id > 0)
                {
                    flag= true;
                    
                }
            }
            if(flag)
            {
                this.InitInfo();
            }
        }

        #endregion

        #region 编辑

        private IRelayCommand editCommand;

        public IRelayCommand EditCommand
        {
            get { return editCommand ??= new RelayCommand<Gift>(Edit); }
        }

        private void Edit(Gift gift)
        {
            if(gift == null)
            {
                return;
            }
            this.DataM.NewVisibility = Visibility.Visible;
            this.DataM.NewGift= gift;
        }

        #endregion

        #region 删除

        private IRelayCommand delCommand;

        public IRelayCommand DelCommand
        {
            get { return delCommand ??= new RelayCommand<Gift>(Del); }
        }

        private void Del(Gift gift)
        {
            if(gift == null)
            {
                return;
            }
            if (gift.Id < 1)
            {
                return;
            }
            var entity = new GiftEntity()
            {
                Id = gift.Id,
                Name = gift.Name,
                ImageUrl = gift.ImageUrl,
            };
            int cnt = this.giftDal.DeleteGift(entity);
            if(cnt > 0)
            {
                InitInfo();
            }
        }

        #endregion
    }
}
